package com.dongqi.myblog.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CannedAccessControlList;
import com.dongqi.myblog.service.OssService;
import com.dongqi.myblog.util.ConstantPropertiesUtil;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Service
public class OssServiceImpl implements OssService {

    //上传文件到oss
    @Override
    public String uploadFileAvatar(MultipartFile file) {

        //获取阿里云存储相关常量
        String endPoint = ConstantPropertiesUtil.END_POINT;
        String accessKeyId = ConstantPropertiesUtil.ACCESS_KEY_ID;
        String accessKeySecret = ConstantPropertiesUtil.ACCESS_KEY_SECRET;
        String bucketName = ConstantPropertiesUtil.BUCKET_NAME;
        //String fileHost = ConstantPropertiesUtil.FILE_HOST;
        //返回的路径，最后手动拼接
        String uploadUrl = null;

        try {
            //判断oss实例是否存在：如果不存在则创建，如果存在则获取
            OSS ossClient = new OSSClient(endPoint, accessKeyId, accessKeySecret);

            if (!ossClient.doesBucketExist(bucketName)) {
                //创建bucket
                ossClient.createBucket(bucketName);
                //设置oss实例的访问权限：公共读
                ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
            }

            //获取上传文件输入流
            InputStream inputStream = file.getInputStream();

            //构建日期路径：avatar/2019/02/26/文件名
            String filePath = new DateTime().toString("yyyy/MM/dd");
            //文件名：uuid.扩展名
            //获取文件的源文件名
            String original = file.getOriginalFilename();
            String fileName = UUID.randomUUID().toString().replaceAll("-","");
            String fileType = original.substring(original.lastIndexOf("."));
            String newName = fileName + fileType;
            //String fileUrl = fileHost + "/" + filePath + "/" + newName;

            //将文件按照日期进行分类   2020/08/01/1.jpg
            //获取当前的日期 原本需要new Date()，再用SimpleDateFormat进行格式化
            //现在借助joda-time jar包当中的工具类获取当前日期
            String datePath = new DateTime().toString("yyyy/MM/dd");
            //拼接
            newName = datePath+"/"+newName;

            //文件上传至阿里云
            //第一个参数 Bucket名称
            //第二个参数 上传的oss文件的路径以及文件名称
            //比如上传/aa/bb/1.jpg oss会先创建/aa/bb文件夹，再在文件夹当中上传文件
            //第三个参数 上传文件的输入流
            ossClient.putObject(bucketName, newName, inputStream);

            // 关闭OSSClient。
            ossClient.shutdown();

            //需要将上传到阿里云的oss路径手动拼接起来并且返回
            //获取url地址
            //https://edu-guli-2020-07-31.oss-cn-shanghai.aliyuncs.com/1.jpg
            uploadUrl = "https://" + bucketName + "." + endPoint + "/" + newName;

        } catch (IOException e) {
            e.printStackTrace();
            //throw new GuliException(ResultCodeEnum.FILE_UPLOAD_ERROR);
        }
        return uploadUrl;
    }

}
